package cn.iocoder.yudao.module.famous.doctor.service.researchgroupuser;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.attention.vo.AttentionPageRespVO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.dal.mysql.user.MemberUserMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.researchgroupuser.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.researchgroupuser.ResearchgroupUserDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.famous.doctor.convert.researchgroupuser.ResearchgroupUserConvert;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.researchgroupuser.ResearchgroupUserMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.famous.doctor.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 课题组-用户关联 Service 实现类
 *
 * @author 启航
 */
@Service
@Validated
public class ResearchgroupUserServiceImpl implements ResearchgroupUserService {

    @Resource
    private ResearchgroupUserMapper researchgroupUserMapper;

    @Resource
    private MemberUserMapper memberUserMapper;
    @Override
    public Long createResearchgroupUser(ResearchgroupUserCreateReqVO createReqVO) {
        // 插入
        ResearchgroupUserDO researchgroupUser = ResearchgroupUserConvert.INSTANCE.convert(createReqVO);
        researchgroupUserMapper.insert(researchgroupUser);
        // 返回
        return researchgroupUser.getId();
    }


    @Override
    public Long createResearchgroupUserList(List<ResearchgroupUserCreateReqVO> createReqVO) {
        if (createReqVO.size()>0) {
            for (int i = 0; i < createReqVO.size(); i++) {
                ResearchgroupUserDO researchgroupUser = ResearchgroupUserConvert.INSTANCE.convert(createReqVO.get(i));
                researchgroupUserMapper.insert(researchgroupUser);
            }
            return 1L;
        }
        return 0L;
    }

    @Override
    public IPage<ResearchgroupUserByClinic> getgroupDockerPage(ResearchgroupUserPageReqVO pageVO) {
        IPage<ResearchgroupUserByClinic> researchgroupUserByClinicIPage = researchgroupUserMapper.selectClinicPage(MyBatisUtils.buildPage(pageVO), pageVO);
        researchgroupUserByClinicIPage.getRecords().forEach(researchgroupUserByClinic -> {
            if (researchgroupUserByClinic.getUserType()==3) {
                //医生id
                researchgroupUserByClinic.setUserDO(memberUserMapper.selectById(researchgroupUserByClinic.getUserSign()));
            } else if  (researchgroupUserByClinic.getUserType()==4){
                //患者身份证
                researchgroupUserByClinic.setUserDO(memberUserMapper.selectById(researchgroupUserByClinic.getUserSign()));
            }
        });
        return researchgroupUserByClinicIPage;
    }

    @Override
    public void updateResearchgroupUser(ResearchgroupUserUpdateReqVO updateReqVO) {
        // 校验存在
        validateResearchgroupUserExists(updateReqVO.getId());
        // 更新
        ResearchgroupUserDO updateObj = ResearchgroupUserConvert.INSTANCE.convert(updateReqVO);
        researchgroupUserMapper.updateById(updateObj);
    }

    @Override
    public void deleteResearchgroupUser(Long id) {
        // 校验存在
        validateResearchgroupUserExists(id);
        // 删除
        researchgroupUserMapper.deleteById(id);
    }

    private void validateResearchgroupUserExists(Long id) {
        if (researchgroupUserMapper.selectById(id) == null) {
            throw exception(RESEARCHGROUP_USER_NOT_EXISTS);
        }
    }

    @Override
    public ResearchgroupUserDO getResearchgroupUser(Long id) {
        return researchgroupUserMapper.selectById(id);
    }

    @Override
    public List<ResearchgroupUserDO> getResearchgroupUserList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return researchgroupUserMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ResearchgroupUserDO> getResearchgroupUserPage(ResearchgroupUserPageReqVO pageReqVO) {
        return researchgroupUserMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ResearchgroupUserDO> getResearchgroupUserList(ResearchgroupUserExportReqVO exportReqVO) {
        return researchgroupUserMapper.selectList(exportReqVO);
    }


}
